package leilib.ir.dns;

import static org.junit.Assert.*;

import java.net.MalformedURLException;

import leilib.ir.crawler.url.CrawlURL;

import org.junit.Before;
import org.junit.Test;

public class DNSBufferTest {
	DNSBuffer dns;
	
	String host1 = "wwW.baidu.com";
	String ip1 = "222.13.26.202";
	String host2 = "www.scut.edu.cn";
	String ip2 = "202.112.33.11";
	
	String host3 = "www.google.com";

	@Before
	public void setUp() throws Exception {
		this.dns = new DNSBuffer();
		this.dns.addReflection(this.host1,this.ip1 );
		this.dns.addReflection(this.host2,this.ip2 );
		//System.out.println(this.dns.toString());
	}

	@Test
	public void testNDSParser() {
		CrawlURL url=null;
		try {
			url = new CrawlURL("http://"+this.host1+"/index.html");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		String ip =this.dns.DNSParser(url, true);
		//System.out.println(ip);
		assertEquals(ip, this.ip1);
		
	}

	@Test
	public void testGetHostByIP() {
		String host1 = this.dns.getHostByIP(this.ip1);
		String host2 = this.dns.getHostByIP(this.ip2);
		
		assertEquals(host1, this.host1.toLowerCase());
		assertEquals(host2,this.host2.toLowerCase());
		
		String host3 = this.dns.getHostByIP("222.16.62.192");
		assertNotNull(host3);
		System.out.println(host3);
	}

	@Test
	public void testGetIPByHost() {
		String ip1 = this.dns.getIPByHost(this.host1);
		String ip2 = this.dns.getIPByHost(this.host2);
		
		assertEquals(ip1,this.ip1);
		assertEquals(ip2,this.ip2);
	}

	@Test
	public void testIsIP() {
		assertTrue(DNSBuffer.isIP(this.ip1));
		assertTrue(DNSBuffer.isIP(this.ip2));
		
		assertTrue(DNSBuffer.isIP("222.16.62.192"));
		assertTrue(DNSBuffer.isIP("1.0.0.125"));
		assertTrue(DNSBuffer.isIP("1.0.0.253"));
		assertTrue(DNSBuffer.isIP("1.0.0.0"));
		
		assertFalse(DNSBuffer.isIP("222.222.222.333"));
		assertFalse(DNSBuffer.isIP("0.222.222.222"));
		assertFalse(DNSBuffer.isIP("222.222.222.256"));
		
	}

}
